package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.DeptMapper;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.service.IDeptService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Resource
    private DeptMapper deptMapper;

    @Autowired
    private WxCpService wxCpService;

    @Override
    public void deleteDept(Integer deptId) {
        Dept dept = deptMapper.selectById(deptId);

        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + dept.getId() + "]%");
        List<Dept> subDepts = deptMapper.selectList(wrapper);
        for (Dept temp : subDepts) {
            temp.deleteById();
        }

        dept.deleteById();
    }


    @Override
    public boolean insert(Dept entity) {
        super.insert(entity);
        //在企业微信中新增部门
        //Department wxDept = entity.convertWxDept();
        //Departments.defaultDepartments().create(wxDept);
        return true;
    }

    @Override
    public boolean deleteById(Serializable id) {
        //在企业微信中删除部门
        //Departments.defaultDepartments().delete((Integer) id);
        return super.deleteById(id);
    }

    @Override
    public boolean updateById(Dept entity) {
        //在企业微信中更新部门
        //Department wxDept = entity.convertWxDept();
        //Departments.defaultDepartments().update(wxDept);

        return super.updateById(entity);
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public List<WxCpDepart> syscWxDept() throws WxErrorException {
        List<WxCpDepart> list;
        list = wxCpService.getDepartmentService().list(null);

        List<Integer> deptIdList = new ArrayList<>();
        for (WxCpDepart wxDept : list) {
            deptIdList.add(wxDept.getId());
            Dept dept = new Dept();
            dept.setId(wxDept.getId());
            dept.setFullname(wxDept.getName());

            dept.setNum(wxDept.getOrder().intValue());
            dept.setPid(wxDept.getParentId());
            deptSetPids(dept);

            Dept oldDept = this.selectById(wxDept.getId());
            if (oldDept != null) {
                dept.setSimplename(oldDept.getSimplename());
            }else{
                dept.setSimplename(wxDept.getName());
            }
            this.insertOrUpdate(dept);
        }
        deptMapper.deleteOutOf(deptIdList);

        return list;
    }

    private void deptSetPids(Dept dept) {
        if (ToolUtil.isEmpty(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid(0);
            dept.setPids("[0],");
        } else {
            int pid = dept.getPid();
            Dept temp = this.selectById(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }

    @Override
    public List<Map<String, Object>> list(String condition) {
        return this.baseMapper.list(condition);
    }
}
